package com.qdlc.p2p.manage.action.additional;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.BaseGiveAwardService;
import com.qdlc.p2p.biz.service.UpRateService;
import com.qdlc.p2p.biz.service.UserService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.enums.EnumActicityPlanNid;
import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.BaseGiveAwardModel;
import com.qdlc.p2p.dal.model.RateInterestModel;

/**
 *加息券记录相关action
 * @author wjx
 * @version 2.0
 * @date 2015年5月4日 下午2:37:19
 */
public class ManageRateInterestAction extends BaseAction<RateInterestModel> implements ModelDriven<RateInterestModel>{
	
	@Resource
	private UpRateService upRateService;

	@Resource
	private UserService userService;

	@Resource
	@Qualifier("giveUserUpRateService")
	private BaseGiveAwardService giveUserUpRateService;

	private Map<String, Object> data;

	/**
	 * 跳转加息券发放列表
	 * @return
	 */
	@Action("/modules/additional/upRate/allRateInterest")
	public String allRateInterest(){
		return "allRateInterestList";
	}
	
	/**
	 * 异步加载加息券列表
	 * @throws IOException
	 */
	@Action("/modules/additional/upRate/rateInterestJson")
	public void rateInterestJson() throws IOException{
		data = new HashMap<String, Object>();
		upRateService.getRateInteModelList(model);
		PageDataList<RateInterestModel> list = upRateService.getRateInteModelList(model);
		int total = list.getPage().getTotal();
		data.put("total", total);
		data.put("rows", list.getList());
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 禁用
	 * @throws IOException 
	 * 
	 */
	@Action("/modules/additional/upRate/rateIntereDisable")
	public void rateIntereDisable() throws IOException{
		data = new HashMap<String, Object>();
		long id = paramLong("id");
		int result = upRateService.updateRateInterest(id, RateInterestModel.RATE_STATUS_FAIL);
		if (result ==1) {
			data.put("result", "true");
			data.put("msg", MessageUtil.getMessage("MS103001006"));
		}else{
			data.put("result", "false");
			data.put("msg", MessageUtil.getMessage("MF103001007"));
		}
		printJson(getStringOfJpaObj(data));
	}

	/**
	 * 后台添加加息劵
	 */
	@Action("/modules/additional/upRate/upRateDoAppend")
	public void upRateDoAppend() throws Exception{
		data = new HashMap<String, Object>();
		BaseGiveAwardModel baseGiveAwardModel =new BaseGiveAwardModel();
		User user = userService.getUserByUserName(model.getUserName());
		if(user==null)
			throw new UserException("用户不存在");
		baseGiveAwardModel.setUser(user);
		baseGiveAwardModel.setRuleId(model.getRateRuleId());
		baseGiveAwardModel.setRuleType(3);
		baseGiveAwardModel.setSourceType(EnumActicityPlanNid.SYSTEM.getValue());
		giveUserUpRateService.giveAward(baseGiveAwardModel);
		data.put("result", "true");
		data.put("msg", MessageUtil.getMessage("MS102001001"));
		printJson(getStringOfJpaObj(data));
	}
}
